package com.sanbi.common.util.jwt;

import com.sanbi.common.util.DateTimeUtil;
import com.sanbi.common.vo.Enums;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @desc jwt token生成工具
 * @author wanggang
 * @time 2024/9/12 11:39
 */
@Slf4j
public class JwtTokenUtil {
    //180d 过期
    public static String generateClientToken(long userId) {
        JwtHelper jwtHelper = new JwtHelper();
        return jwtHelper.createToken(userId, Enums.TokenTypeEnum.ClientToken);
    }

    //1d 过期
    public static String generateSaleToken(long userId) {
        JwtHelper jwtHelper = new JwtHelper();
        return jwtHelper.createToken(userId, Enums.TokenTypeEnum.SaleToken);
    }

    //1d 过期
    public static String generateWareToken(long userId) {
        JwtHelper jwtHelper = new JwtHelper();
        return jwtHelper.createToken(userId, Enums.TokenTypeEnum.WareToken);
    }

    //180d 过期
	public static String generateToken(long userId) {
        JwtHelper jwtHelper = new JwtHelper();
        return jwtHelper.createToken(userId, Enums.TokenTypeEnum.ClientToken);
    }

    public static Long getUserId(String token) {
    	JwtHelper jwtHelper = new JwtHelper();
        Long userId = jwtHelper.verifyTokenAndGetUserId(token);
    	if(userId == null || userId == 0){
    		return null;
    	}
        return userId;
    }

    public static Boolean verifyToken(String token) {
        JwtHelper jwtHelper = new JwtHelper();
        Long userId = jwtHelper.verifyTokenAndGetUserId(token);
        if(userId != null){
            return true;
        }

        return false;
    }

    public static LocalDateTime getExpireTime(String token) {
        JwtHelper jwtHelper = new JwtHelper();
        Date expireTime = jwtHelper.getExpireTime(token);

        return expireTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static void main(String[] args) {
        long userId=DateTimeUtil.getCurrentTimestamp();
        String saleToken=generateSaleToken(userId);
        String clientToken=generateClientToken(userId);
        LocalDateTime expiredTimeSale=getExpireTime(saleToken);
        LocalDateTime expiredTimeClient=getExpireTime(clientToken);

        System.out.println("sale token: "+saleToken);
        System.out.println("client token: "+clientToken);
        System.out.println("sale token expired time: "+expiredTimeSale);
        System.out.println("client token expired time: "+expiredTimeClient);
    }
}
